package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BaseException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        // 设置 创建时间
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        // 方案2： 先查，再替换 ，再更新  ---->  有条件判断的时候使用较多

        // 方案1：删除不需要修噶改的字段
        Appointment result = this.get(appointment.getId());
        // 不是预约状态不能编辑
        if (result == null || !result.getStatus().equals(Appointment.STATUS_APPOINTMENT)){
            throw new BaseException("非预约状态不能编辑");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancelAppointment(Long id) {
        //取消预约： 修改状态为 取消预约, 做判断，只有在预约状态可以取消
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (!appointment.getStatus().equals(Appointment.STATUS_APPOINTMENT)){
            throw new RuntimeException("非法操作");
        }
        appointment.setStatus(Appointment.STATUS_CANCEL);
        appointmentMapper.changeStatus(appointment);
    }

    @Override
    public void arrive(Long id) {
        // 到店： 修改状态为 到店, 添加到店时间的信息
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        appointment.setActualArrivalTime(new Date());
        appointment.setStatus(Appointment.STATUS_ARRIVAL);
        appointmentMapper.changeStatus(appointment);
    }

    @Override
    public void softDelete(Long id) {
        // 设置 exist 的值为false
        appointmentMapper.softDelete(id, Appointment.EXIST_FALSE);
    }

    // 自己实现------
    /*@Override
    public Long generateStatement(Long id) {
        Statement statement = statementService.selectByAppointmentId(id);
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (statement == null){
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setServiceType(appointment.getServiceType().longValue());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setAppointmentId(id);
            statementService.save(statement);
            // 把预约的状态 改为生成结算单
            appointment.setStatus(Appointment.STATUS_SETTLE);
            appointmentMapper.changeStatus(appointment);
        }
        return statement.getId();
    }*/

    // 课堂实现------
    @Override
    public Long generateStatement(Long id) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        Statement statement = null;
        // 如果为 已结算状态： 查询结算单，返回结算单id
        if (appointment.getStatus().equals(Appointment.STATUS_SETTLE)){
            statement =  statementService.selectByAppointmentId(id);
        }else {
        // 如果为 到店状态： 则根据预约单生成 结算单，在返回结算单id
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setServiceType(appointment.getServiceType().longValue());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setAppointmentId(id);
            statementService.save(statement);
            // 把预约的状态 改为生成结算单
            appointment.setStatus(Appointment.STATUS_SETTLE);
            appointmentMapper.changeStatus(appointment);
        }
        return statement.getId();
    }


    @Override
    public Long selectStatement(Long id) {
        Statement statement = statementService.selectByAppointmentId(id);
        if (statement == null){
            throw new BaseException("结算单不存在");
        }
        return statement.getId();
    }

}
